package com.defangit.production.orm;

import com.defangit.production.bll.ManufactureBll;
import com.defangit.production.domain.Manufacture;
import com.defangit.production.domain.Operator;
import laogen.hibernate.CommonFinder;
import laogen.hibernate.CommonService;
import laogen.lang.exception.ParameterNotValidException;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;


public class ManufactureService extends CommonService<Manufacture> implements ManufactureBll{

    public Manufacture addManufacture(Manufacture manufacture){
        if(manufacture==null) throw ParameterNotValidException.nullParameter("要添加的产品");
        add(manufacture);
        commitSession();
        return get(manufacture.getId()).orElseThrow(()-> ParameterNotValidException.zeroIdOrEntityNonExist("加工品不存在"));
    }

    @Override
    public boolean updateManufacture(Manufacture manufacture) {
        if(manufacture==null) throw ParameterNotValidException.nullParameter("要修改的产品");
        merge(manufacture);
        commitSession();
        return true;
    }

    public Manufacture fixManufactureMessage(Manufacture manufacture,Integer operatorId) {
        if (manufacture == null) throw ParameterNotValidException.nullParameter("需要更新的产品");
        Manufacture target = get(manufacture.getId()).orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("产品信息异常"));
        Operator operator = new OperatorService().get(operatorId).orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("用户信息异常"));
        if (!operator.getTags().contains(2)) throw ParameterNotValidException.constraintNotSatisfy("用户权限不足","您的权限不足");
        if (manufacture.getCraft()!=null) {
            target.setCraft(manufacture.getCraft());
        }
        if (manufacture.getNote()!=null) {
            target.setNote(manufacture.getNote());
        }
        if (manufacture.getSpec()!=null) {
            target.setSpec(manufacture.getSpec());
        }
        update(target);
        commitSession();
        return target;
    }

    @Override
    public boolean deleteManufacture(int id) {
        return false;
    }

    @Override
    public ManufactureFinder findUsed() {
        return findAll().used(true);
    }

    @Override
    public ManufactureFinder findAll() {
        return new ManufactureFinderImp();
    }

    public class ManufactureFinderImp extends CommonFinder<Manufacture> implements ManufactureFinder {
        private DetachedCriteria criteria;
        ManufactureFinderImp(){
            criteria=detachedAll();
        }
        @Override
        public Criteria getCriteria() {
            return criteria.getExecutableCriteria(getSession()).addOrder(Order.desc("id"));
        }

        @Override
        public DetachedCriteria getDetachedCriteria() {
            return criteria;
        }

        @Override
        public ManufactureFinder used(Boolean used) {
            if(used==null) return this;
            criteria=criteria.add(Restrictions.eq("notUsed",!used));
            return this;
        }
    }
}
